package com.itheima.day12_多线程.Test3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
3、【编号2003】某电影院目前正在上映国产大片，共有100张票，
而它有3个窗口卖票，请设计一个程序模拟该电影院卖票，
并保证线程同步，不会出现错误的买票数据。
 */
public class Ticket implements Runnable {
    private int ticket = 100;
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {

        while (true) {
            lock.lock();
            try {
                if (ticket > 0) {
                    Thread.sleep(100);

                ticket--;
                System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket + "张票");}
                else{
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}


